package com.wswy.wzcx.module

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.util.Log
import com.che.common.map.LocationModel
import com.che.libcommon.utils.RxBus
import com.google.gson.reflect.TypeToken
import com.wswy.wzcx.api.ErrorCode
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.module.base.UserViewModel
import com.wswy.wzcx.module.base.onResult
import io.reactivex.observers.ResourceObserver

open class CarsViewModel : UserViewModel() {

    private val userCarsLiveData = MutableLiveData<Resource<List<UserCarInfo>?>>()

    override fun onCreate() {
        super.onCreate()
        registerObservers()
    }

    override fun registerObservers() {
        super.registerObservers()

        val userCarInfoObserver = object : ResourceObserver<UserCarInfo>() {
            override fun onNext(userCarInfo: UserCarInfo) {
                onUserCarInfoUpdate(userCarInfo)
            }

            override fun onError(e: Throwable) {

            }

            override fun onComplete() {

            }
        }


        val userCarExtendInfoObserver = object : ResourceObserver<UserCarInfo>() {
            override fun onNext(userCarInfo: UserCarInfo) {
                onUserCarExtendInfoUpdate(userCarInfo)
            }

            override fun onError(e: Throwable) {

            }

            override fun onComplete() {

            }
        }

        val userCarDeleteObserver = object : ResourceObserver<UserCarInfo>() {
            override fun onNext(userCarInfo: UserCarInfo) {
                onDeleteCar(userCarInfo.id)
            }

            override fun onError(e: Throwable) {

            }

            override fun onComplete() {

            }
        }







        RxBus.getDefault().toObservableWithCode(InternalMsgCode.CODE_ADD_UPDATE,
                UserCarInfo::class.java)
                .subscribe(userCarInfoObserver)

        RxBus.getDefault().toObservableWithCode(InternalMsgCode.CODE_UPDATE_EXTEND,
                UserCarInfo::class.java)
                .subscribe(userCarExtendInfoObserver)


        RxBus.getDefault().toObservableWithCode(InternalMsgCode.CODE_DELETE, UserCarInfo::class.java)
                .subscribe(userCarDeleteObserver)


        addRun(userCarInfoObserver)
        addRun(userCarExtendInfoObserver)

        addRun(userCarDeleteObserver)

    }

    fun loadMyCars(forceApi:Boolean=false){
        userCarsLiveData.postValue(Resource.loading())

        if (hasLogin()){

            UserDataProvider.get().myCars(forceApi).onResult({
                onMyCars(it)
            },{
                if (ErrorCode.isTokenInvalid(it)) {
                    userCarsLiveData.postValue(Resource.error("请重新登录"))
                } else {
                    if (!forceApi) {
                        onMyCars(null)
                    } else{
                        userCarsLiveData.postValue(Resource.error("请求失败，请重试"))
                    }
                }
            })
        }else{
            userCarsLiveData.postValue(Resource.error("请先登录"))
        }
    }

    open fun onUserCarExtendInfoUpdate(userCarInfo: UserCarInfo){

    }

    open fun onUserCarInfoUpdate(userCarInfo: UserCarInfo){

    }

    open fun onDeleteCar(carId:Int){

    }

    private fun onMyCars(mycars:List<UserCarInfo>?){

        userCarsLiveData.postValue(Resource.success(mycars))
    }

    fun getUserCarsLiveData():LiveData<Resource<List<UserCarInfo>?>> = userCarsLiveData



}